package bookmanage;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class BookDao {
    // 1. 新增书籍(添加)
    public boolean add(Book book){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 拼装 SQL 语句
            String sql = "insert into book values(null,?,?,?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, book.getName());
            statement.setString(2, book.getAuthor());
            statement.setString(3, book.getType());
            statement.setInt(4,book.getPrice());
            statement.setInt(5, book.isBorrowed()? 1 : 0);
            int ret = statement.executeUpdate();
            if(ret != 1){
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null );
        }
        return false;
    }
    // 2. 查看所有书籍(查找)
    public List<Book> selectAll(){
        List<Book> books = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 拼装 SQL 语句
            String sql = "select * from book";
            statement = connection.prepareStatement(sql);
            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 遍历结果集合
            while (resultSet.next()){
                // 取出当前的这些列， 构造出一个Book对象来
                Book book = new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setType(resultSet.getString("type"));
                book.setPrice(resultSet.getInt("price"));
                book.setBorrowed(resultSet.getInt("isBorrowed") == 1);
                // 将获取到的对象放入ArrayList
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return books;
    }
    // 3. 根据名字查找书籍(查找)
    public List<Book> selectByName(String name){
        List<Book> books = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            // 与数据库建立连接
            connection = DBUtil.getConnection();
            // 拼装 SQL 语句
            String sql = "select * from book where name =?";
            statement = connection.prepareStatement(sql);
            statement.setString(1,name);
            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 遍历结果
            while(resultSet.next()){
                Book book = new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setType(resultSet.getString("type"));
                book.setPrice(resultSet.getInt("price"));
                book.setBorrowed(resultSet.getInt("isBorrowed")==1);
                books.add(book);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return books;
    }
    // 4. 删除书籍(删除)
    public boolean delete (int bookId){
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DBUtil.getConnection();
            String sql = "delete from book where bookId =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            int ret  = statement.executeUpdate();
            if (ret != 0){
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }
    // 5. 借书(修改)
    public boolean borrowBook(int bookId){
        Connection connection =null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            // 如果这里的 isBorrowed 已经为 1 还能借吗？
            // 如果这里的bookId不存在 还能借吗？
            // [这里先查找书籍判断是否存在]
            String sql ="select * from book where bookId =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            // 执行 SQL 语句
            resultSet = statement.executeQuery();
            // 此时无非就 1条记录 或者 0条记录
            if (resultSet.next()){
                boolean isBorrowed = (resultSet.getInt("isBorrowed") == 1);
                if (isBorrowed){
                    System.out.println("书已经借出！ 无法再次借出 bookID = " +bookId);
                    return false;
                }
            }else {
                System.out.println("书不存在！ 无法借出 bookId =" + bookId);
                return false;
            }
             //开始进行借书逻辑
            sql = "update book set isBorrowed = 1 where bookId =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            int ret = statement.executeUpdate();
            if (ret != 0){
                return true;
            }
            return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }
    // 6. 还书(修改)
    public boolean returnBook(int bookId){
        Connection connection =null;
        PreparedStatement statement = null;
        ResultSet resultSet =null;
        try {
            connection = DBUtil.getConnection();
            String sql ="select * from book where bookId =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            resultSet = statement.executeQuery();
            if (resultSet.next()){
                  boolean isBorrowed = (resultSet.getInt("isBorrowed")==1);
                  if (!isBorrowed){
                      System.out.println("书没有借出！！");
                      return false;
                  }
            }else {
                System.out.println(" 该书籍不存在！ ");
                return false;
            }
            sql ="update book set isBorrowed=0 where bookId =?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            // 执行 SQL 语句
            int ret = statement.executeUpdate();
            if (ret != 0 ){
                return true;
            }
            return false ;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
        return false;
    }
}
